package com.ec.pf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ec.pf.pojo.FamilyProfile;
import com.ec.pf.service.FamilyProfileService;
import com.ec.common.domain.Result;
import com.ec.common.exception.DbException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/family")
public class FamilyProfileController {

    @Autowired
    private FamilyProfileService familyProfileService;

    /**
     * 新增家属档案
     */
    @PostMapping
    public Result<FamilyProfile> addFamilyProfile(@RequestBody FamilyProfile familyProfile) {
        try {
            // 设置 created_at 和 updated_at 为当前时间
            Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
            familyProfile.setCreatedAt(currentTimestamp);
            familyProfile.setUpdatedAt(currentTimestamp);

            FamilyProfile result = familyProfileService.addFamilyProfile(familyProfile);
            return Result.ok(result);
        } catch (Exception e) {
            throw new DbException("数据库错误: " + e.getMessage());
        }
    }

    /**
     * 根据ID删除家属档案
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteFamilyProfile(@PathVariable Long id) {
        try {
            boolean success = familyProfileService.deleteFamilyProfileById(id);
            if (success) {
                return Result.ok();
            } else {
                throw new DbException("删除失败，未找到该家属档案");
            }
        } catch (Exception e) {
            throw new DbException("数据库错误: " + e.getMessage());
        }
    }

    /**
     * 修改家属档案
     */
    @PutMapping("/{id}")
    public Result<FamilyProfile> updateFamilyProfile(@PathVariable Long id, @RequestBody FamilyProfile familyProfile) {
        try {
            // 设置 ID
            familyProfile.setId(id);

            // 显式设置 updatedAt 字段
            familyProfile.setUpdatedAt(new Timestamp(System.currentTimeMillis()));

            // 调用 service 层更新家属档案
            FamilyProfile result = familyProfileService.updateFamilyProfile(familyProfile);
            return Result.ok(result);
        } catch (Exception e) {
            throw new DbException("数据库错误: " + e.getMessage());
        }
    }

    /**
     * 根据ID查询家属档案
     */
    @GetMapping("/{id}")
    public Result<FamilyProfile> getFamilyProfile(@PathVariable Long id) {
        try {
            FamilyProfile familyProfile = familyProfileService.getFamilyProfileById(id);
            return Result.ok(familyProfile);
        } catch (Exception e) {
            throw new DbException("数据库错误: " + e.getMessage());
        }
    }

    /**
     * 分页查询家属档案
     */
    @GetMapping("/list")
    public Result<Page<FamilyProfile>> listFamilyProfiles(@RequestParam Integer page, @RequestParam Integer size) {
        try {
            Page<FamilyProfile> familyProfiles = familyProfileService.listFamilyProfiles(page, size);
            return Result.ok(familyProfiles);
        } catch (Exception e) {
            throw new DbException("数据库错误: " + e.getMessage());
        }
    }

    /**
     * 批量删除家属档案
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteFamilyProfiles(@RequestBody List<Long> ids) {
        try {
            boolean success = familyProfileService.batchDeleteFamilyProfiles(ids);
            if (success) {
                return Result.ok();
            } else {
                throw new DbException("批量删除失败");
            }
        } catch (Exception e) {
            throw new DbException("数据库错误: " + e.getMessage());
        }
    }

    /**
     * 模糊查询家属档案
     */
    @GetMapping("/search")
    public Result<Page<FamilyProfile>> searchFamilyProfiles(@RequestParam(required = false) String fullName,
                                                            @RequestParam(required = false) String gender,
                                                            @RequestParam(required = false) String phone,
                                                            @RequestParam int page,
                                                            @RequestParam int size) {
        try {
            // 调用 service 层方法进行模糊查询
            Page<FamilyProfile> familyProfiles = familyProfileService.searchFamilyProfiles(fullName, gender, phone, page, size);
            return Result.ok(familyProfiles);
        } catch (Exception e) {
            throw new DbException("数据库错误: " + e.getMessage());
        }
    }
}
